package com.bifang.core.dao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bifang.common.constant.enums.common.BoolEnum;
import com.bifang.common.constant.enums.common.CommentTypeEnum;
import com.bifang.common.constant.enums.common.GetApplyTasksEnum;
import com.bifang.common.util.CommonSecureUtil;
import com.bifang.common.vo.HomePageOperationVO;
import com.bifang.common.vo.OplogQueryVO;
import com.bifang.common.vo.OplogUpdateDelSqlVO;
import com.bifang.common.vo.OplogVO;
import com.bifang.common.vo.OrderVO;
import com.bifang.core.dao.mapper.OplogMapper;
import com.bifang.core.dao.model.Oplog;
import com.bifang.core.dao.service.OplogService;
import java.math.BigDecimal;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * 业务日志主表 服务实现类
 *
 * @author chenzh
 * @since 2020-06-13
 */
@Service
public class OplogServiceImpl extends ServiceImpl<OplogMapper, Oplog> implements OplogService {

    @Autowired private OplogMapper oplogMapper;

    @Value("${system.config.user.encryption.flag}")
    private Boolean encryptionFlag;

    @Override
    public Oplog getByParentId(Long parentId) {
        QueryWrapper<Oplog> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .lambda()
                .eq(Oplog::getParentId, parentId)
                .eq(Oplog::getStatus, BoolEnum.TRUE.getType())
                .eq(Oplog::getRbStatus, BoolEnum.FALSE.getType());
        return this.getOne(queryWrapper);
    }

    @Override
    public List<Oplog> getByBusNoAndActId(BigDecimal busNo, List<String> actIds) {
        QueryWrapper<Oplog> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .lambda()
                .eq(Oplog::getBusNo, busNo)
                // .ne(Oplog::getAuthStatus, AuthStatusEnum.BH.getType())
                .ne(Oplog::getCommentType, CommentTypeEnum.BH)
                .eq(Oplog::getStatus, BoolEnum.TRUE.getType())
                .eq(Oplog::getRbStatus, BoolEnum.FALSE.getType())
                .in(Oplog::getActId, actIds)
                .orderByAsc(Oplog::getOpTime);
        return this.list(queryWrapper);
    }

    @Override
    public List<OplogVO> getHandleTasks(OplogQueryVO oplogQueryVO) {
        if (encryptionFlag) {
            oplogQueryVO.setApplicantId(CommonSecureUtil.aesEncrypt(oplogQueryVO.getApplicantId()));
            oplogQueryVO.setApplicantName(
                    CommonSecureUtil.aesEncrypt(oplogQueryVO.getApplicantName()));
        }
        return oplogMapper.getHandleTasks(oplogQueryVO);
    }

    @Override
    public List<OplogVO> getFinishedTasks(OplogQueryVO oplogQueryVO) {
        if (encryptionFlag) {
            oplogQueryVO.setApplicantId(CommonSecureUtil.aesEncrypt(oplogQueryVO.getApplicantId()));
            oplogQueryVO.setApplicantName(
                    CommonSecureUtil.aesEncrypt(oplogQueryVO.getApplicantName()));
        }
        return oplogMapper.getFinishedTasks(oplogQueryVO);
    }

    @Override
    public List<OplogVO> getAllApplyTasks(OplogQueryVO oplogQueryVO) {
        if (!CollectionUtils.isEmpty(oplogQueryVO.getOrderVOList())) {
            for (OrderVO orderVO : oplogQueryVO.getOrderVOList()) {
                GetApplyTasksEnum getApplyTasksEnum =
                        GetApplyTasksEnum.getByType(orderVO.getProperty());
                if (getApplyTasksEnum != null) {
                    String text = getApplyTasksEnum.getText();
                    orderVO.setProperty(text);
                }
            }
        }
        return oplogMapper.getAllApplyTasks(oplogQueryVO);
    }

    @Override
    public List<OplogVO> getApplyedTasks(OplogQueryVO oplogQueryVO) {
        if (!CollectionUtils.isEmpty(oplogQueryVO.getOrderVOList())) {
            for (OrderVO orderVO : oplogQueryVO.getOrderVOList()) {
                GetApplyTasksEnum getApplyTasksEnum =
                        GetApplyTasksEnum.getByType(orderVO.getProperty());
                if (getApplyTasksEnum != null) {
                    String text = getApplyTasksEnum.getText();
                    orderVO.setProperty(text);
                }
            }
        }
        if (encryptionFlag) {
            oplogQueryVO.setApplicantId(CommonSecureUtil.aesEncrypt(oplogQueryVO.getApplicantId()));
            oplogQueryVO.setApplicantName(
                    CommonSecureUtil.aesEncrypt(oplogQueryVO.getApplicantName()));
        }
        return oplogMapper.getApplyedTasks(oplogQueryVO);
    }

    @Override
    public void commonUpdateDel(OplogUpdateDelSqlVO vo) {
        oplogMapper.commonUpdateDel(vo);
    }

    @Override
    public List<HomePageOperationVO> getOperationNum(OplogQueryVO vo) {
        return oplogMapper.getOperationNum(vo);
    }

    @Override
    public Integer getApplyedNum(OplogQueryVO vo) {
        return oplogMapper.getApplyedNum(vo);
    }

    @Override
    public void moveOplogData(OplogQueryVO oplogQueryVO) {
        oplogMapper.moveOplogData(oplogQueryVO);
    }

    @Override
    public void moveOplogApiData(OplogQueryVO oplogQueryVO) {
        oplogMapper.moveOplogApiData(oplogQueryVO);
    }

    @Override
    public void moveOplogTableChangeData(OplogQueryVO oplogQueryVO) {
        oplogMapper.moveOplogTableChangeData(oplogQueryVO);
    }

    @Override
    public List<String> getClearOplogData(OplogQueryVO oplogQueryVO) {
        return oplogMapper.getClearOplogData(oplogQueryVO);
    }

    @Override
    public List<String> getClearOplogApiData(OplogQueryVO oplogQueryVO) {
        return oplogMapper.getClearOplogApiData(oplogQueryVO);
    }

    @Override
    public List<String> getClearOplogTableChangeData(OplogQueryVO oplogQueryVO) {
        return oplogMapper.getClearOplogTableChangeData(oplogQueryVO);
    }
}
